package evenements;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import modele.map.MapParser;
import controleur.Horloge;
import vues.CouleurMapWindow;
import vues.IHM;

public class EcouteurBarMenu implements ActionListener{

	IHM ihm;
	Toolkit tk = Toolkit.getDefaultToolkit();
	Image img;
	JFileChooser chooser;
	public EcouteurBarMenu(IHM ihm){
		this.ihm = ihm;
		chooser = new JFileChooser();
		}	
	@Override
	public void actionPerformed(ActionEvent e) {
		switch (e.getActionCommand()){
		case "Sauvegarder la simulation" :
			this.ihm.getHorloge().getSimulateur().save();
			break;
		case "Charger une simulation" :
		    FileFilter filter = new FileNameExtensionFilter("simulation file", "simulation");
		    chooser.setFileFilter(filter);
		    int returnVal = chooser.showOpenDialog(this.ihm);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	this.ihm.getHorloge().getSimulateur().load(chooser.getSelectedFile().getPath());
		    }
			break;
		case "Charger une map":
			 FileNameExtensionFilter filterImg = new FileNameExtensionFilter(
				        "JPG & PNG Images", "jpg", "png");
			chooser.setFileFilter(filterImg);
			int returnVal2 = chooser.showOpenDialog(this.ihm);
			if(returnVal2 == JFileChooser.APPROVE_OPTION) {
		   
				try {
//					MapParser mprser = new MapParser(5, 5, chooser.getSelectedFile().getAbsolutePath());
//					CouleurMapWindow mapWindow = new CouleurMapWindow(mprser.ListeDesCouleurs());
					
					Image backgroundImage = ImageIO.read(chooser.getSelectedFile());
					this.ihm.getPanGrille().setBackgroundImg(backgroundImage);
					this.ihm.refresh();
				} catch (IOException e1) {
					e1.printStackTrace();
				}

		    }
			break;
		case "Quitter":
			System.exit(0);
			break;
        case "Nouvelle carte":
                break;
		case "Créer un robot":
			img = tk.getImage(this.ihm.getClass().getResource("robot-haut-urbain.png"));
			Cursor curseurRobot = tk.createCustomCursor(img, new Point(16, 16), "robot");
			this.ihm.setCursor(curseurRobot);
			break;
		case "Créer un feu":
			img = tk.getImage(this.ihm.getClass().getResource("fire.png"));
			Cursor curseurFeu = tk.createCustomCursor(img, new Point(16, 16), "fire");
			this.ihm.setCursor(curseurFeu);
			break;
		}
		
	}
	

}
